package cn.itmantzy.user.service.impl;

import cn.itmantzy.basic.constant.BaseConstants;
import cn.itmantzy.basic.exception.BusinessException;
import cn.itmantzy.basic.service.impl.BaseServiceImpl;
import cn.itmantzy.basic.util.MD5Utils;
import cn.itmantzy.basic.util.StrUtils;
import cn.itmantzy.user.domain.Logininfo;
import cn.itmantzy.user.domain.User;
import cn.itmantzy.user.dto.UserDto;
import cn.itmantzy.user.mapper.LogininfoMapper;
import cn.itmantzy.user.mapper.UserMapper;
import cn.itmantzy.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 用户接口实现类
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public User queryByPhone(String phone) {
        return userMapper.loadByPhone(phone);
    }

    /**
     * 手机号注册业务实现
     * @param userDto
     */
    @Override
    public void phoneRegister(UserDto userDto) {
        // 1.进行空校验
        if (userDto.getPhone() == null || userDto.getVerifyCode() == null || userDto.getPassword() == null || userDto.getConfirmPassword() == null){
            throw new BusinessException("参数异常！！！");
        }
        // 2.判断两次输入的密码是否一致
        if(!userDto.getPassword().equals(userDto.getConfirmPassword())){
            // 如果两次输入的密码不一致，则抛出异常
            throw new BusinessException("请保证两次输入的密码一致!!!");
        }
        // 判断用户是否已经被注册
        User userTmp = userMapper.loadByPhone(userDto.getPhone());
        if (userTmp != null){
            throw new BusinessException("该用户已存在，请直接登录或点击忘记密码进行找回！！！");
        }
        // 校验验证码是否输入正确
        Object codeTmp = redisTemplate.opsForValue().get(BaseConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        // 判断验证码是否已过期
        if(codeTmp == null){
            throw new BusinessException("验证码已过期，请重新获取！！！");
        }
        // 获取到验证码
        String code = ((String) codeTmp).split(":")[0];
        // 判断验证码是否输入正确
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)){ // 不需要区分大小写
            throw new BusinessException("输入的验证码不一致！！！");
        }
        // 保存注册信息，根据表分析，应该先保存logininfo再保存user
        User user = userDto2User(userDto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        // 给user中的属性赋值
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 盐值加密后的密码
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        // 同名拷贝yuanze
        BeanUtils.copyProperties(user, logininfo);
        return logininfo;
    }

}
